﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using System.Data;
using System.Windows.Forms;

namespace SPMigrationOracleUtility
{
    class Mapping_List
    {
        public int id=0;

        public static string SiteUrl = Convert.ToString(SPMigrationOracleUtility.Appl.Default.SiteURL);
 
        public string MappingName { get; set; }
        public string Desc { get; set; }
        public string AllItems { get; set; }
        public string Query { get; set; }
        public string ViewName { get; set; }

        public string Map_ID { get; set; }
        public string Src_Tbl_Name { get; set; }
        public string Dest_Lst_Name { get; set; }
        public string Src_Fld { get; set; }
        public string Dest_Fld { get; set; }
        public string Src_Fld_Type { get; set; }
        public string Dest_Fld_Type { get; set; }
        public string Web_Url { get; set; }

        /// <summary>
        /// Mapping List Constructor to to make an entry on Mapping List while a new mapping created
        /// </summary>
        /// <param name="name">Mapping Name</param>
        /// <param name="desc">Mapping Description</param>
        /// <param name="allitems">Specify whether allitems/query checked</param>
        /// <param name="query">Specify whether allitems/query checked</param>
        /// <param name="view">Specify the view name if view selected</param>
        public Mapping_List(string name,string desc, string allitems, string query, string view,string filePath)
        {
            MappingName = name;
            Desc = desc;
            AllItems = allitems;
            Query = query;
            ViewName = view;

            using (ClientContext cliContext = new ClientContext(SiteUrl))
            {
                Web web = cliContext.Web;
                List list = web.Lists.GetByTitle("Mapping List");
                cliContext.Load(list);
                cliContext.ExecuteQuery();
                ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                ListItem item = list.AddItem(itemCreateInfo);
                item["Title"] = MappingName;
                item["Description"] = Desc;
                item["All_Items"] = AllItems;
                item["Query"] = Query;
                item["View_Name"] = ViewName;

                if (string.IsNullOrEmpty(filePath))
                {
                    item["MappingMode"] = "Oracle Migration";
                }
                else
                {
                    item["MappingMode"] = "Excel Migration";
                }
                item["ExcelFilePath"] = filePath;
                item.Update();
                cliContext.ExecuteQuery();
            }
        }

        /// <summary>
        /// Get ID of Last added Item
        /// </summary>
        /// <returns>Returns the ID</returns>
        public static string GetID()
        {
            using (ClientContext cliContext = new ClientContext(SiteUrl))
            {
                Web web = cliContext.Web;
                List list = web.Lists.GetByTitle("Mapping List");
                cliContext.Load(list);
                cliContext.ExecuteQuery();
                CamlQuery query = new CamlQuery();
                query.ViewXml="<View><Query><OrderBy><FieldRef Name='Created' Ascending='True' /></OrderBy></Query></View>";
                ListItemCollection itmColl = list.GetItems(query);
                cliContext.Load(itmColl);
                cliContext.ExecuteQuery();
                int count = itmColl.Count-1;
                string itemID = Convert.ToString(itmColl[count]["ID"]);
                return itemID;
            }
        }

        /// <summary>
        /// Add an item in Mapping Column List 
        /// </summary>
        /// <param name="dtTable">DataTable with the values for the fields</param>
        /// <param name="url">Specify the Web Url of the selected list</param>
        public void MapList(DataTable dtTable, string url)
        {
            try
            {
                using (ClientContext cliContext = new ClientContext(SiteUrl))
                {
                    Web web = cliContext.Web;
                    List list = web.Lists.GetByTitle("Mapping Column List");
                    cliContext.Load(list);
                    cliContext.ExecuteQuery();
                    foreach (DataRow dtRow in dtTable.Rows)
                    {
                        ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                        ListItem item = list.AddItem(itemCreateInfo);
                        Map_ID = GetID();
                        item["Mapping_ID"] = Map_ID;
                        item["Source_Table_Name"] = dtRow["SourceTableName"];
                        item["Destination_List_Name"] = dtRow["DestinationListName"];
                        item["Source_Field"] = dtRow["SourceTableColumn"];
                        item["Destination_Field"] = dtRow["DestinationListColumn"];
                        item["Source_Field_Type"] = dtRow["SourceColumnType"];
                        item["Destination_Field_Type"] = dtRow["DestinationColumnType"];
                        item["Web_Url"] = url;
                        item["Destination_List_Template"] = dtRow["DestinationListTemplate"];
                        item.Update();
                        cliContext.ExecuteQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
        
        // for Excel Data Migration
        public void MapList(DataTable dtTable, string url, string name)
        {
            MappingName = name;
            string SiteUrl = url;
            try
            {
                using (ClientContext cliContext = new ClientContext(SiteUrl))
                {
                    Web web = cliContext.Web;
                    List list = web.Lists.GetByTitle("Mapping Column List");
                    cliContext.Load(list);
                    cliContext.ExecuteQuery();
                    foreach (DataRow dtRow in dtTable.Rows)
                    {
                        ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                        ListItem item = list.AddItem(itemCreateInfo);
                        Map_ID = GetID();
                        item["Title"] = MappingName;
                        item["Mapping_ID"] = Map_ID;
                        item["Source_Table_Name"] = dtRow["SourceTableName"];
                        item["Destination_List_Name"] = dtRow["DestinationListName"];
                        item["Source_Field"] = dtRow["SourceTableColumn"];
                        item["Destination_Field"] = dtRow["DestinationListColumn"];
                        item["Source_Field_Type"] = dtRow["SourceColumnType"];
                        item["Destination_Field_Type"] = dtRow["DestinationColumnType"];
                        item["Web_Url"] = url;
                        item["Destination_List_Template"] = dtRow["DestinationListTemplate"];
                        item.Update();
                        cliContext.ExecuteQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}
